﻿using Kn.DLMS;
using Kn.DLMS.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using WindowsFormsApplication3;

namespace Kn.ALICE
{
    #region  信道特征信息 基类
    public class GXAliceChannelFeature
    {

        public ushort Code { get; set; }   //编码
        public ushort Len { get; set; }   //长度
        public string Name { get; set; }         // 名称
        public string strUIParseResult = "";   //解析结果
        public GXByteBuffer dataBuf = new GXByteBuffer();

        //GXByteBuffer ParaBuf = new GXByteBuffer();
        public virtual bool Parser()
        {
            // 可以在派生类中重写该方法
            return true;
        }

    }
    #endregion

    #region COOID
    public class NetworkUnits
    {
        public int CurrentID { get; set; } // 当前网络单元ID



        public List<int> NeighborIDs { get; set; } // 邻域网络单元ID列表

        public int ConvertByteArrayToInt(byte[] bytes)
        {
            if (bytes.Length != 4)
            {
                throw new ArgumentException("字节数组的长度必须为4。");
            }

            //if (BitConverter.IsLittleEndian)
            //{
            //    Array.Reverse(bytes); // 如果系统是小端字节序，需要反转字节数组
            //}

            int value = BitConverter.ToInt32(bytes, 0);
            return value;
        }

        public NetworkUnits()
        {
            NeighborIDs = new List<int>();
        }


    }
    #endregion

    #region 0012 CCO ID或CELL ID
    public class GXGXAliceChannelFeature_0012 : GXAliceChannelFeature
    {
        #region 属性
        public NetworkUnit networkUnit = new NetworkUnit();
        #endregion

        public GXGXAliceChannelFeature_0012()
        {
            Code = 0x0012;
            Name = "信道特征信息编码0012(COO ID 或 CELL ID)";
        }

        public override bool Parser()
        {
            try
            {

                Code = dataBuf.GetUInt16();
                Len = dataBuf.GetUInt16();

                byte[] tmp = new byte[4];

                dataBuf.Get(tmp);

                networkUnit.CurrentID = networkUnit.ConvertByteArrayToInt(tmp);

                int num = dataBuf.GetUInt8();
                for (int i = 0; i < num; i++)
                {
                    dataBuf.Get(tmp);
                    networkUnit.NeighborIDs.Add(networkUnit.ConvertByteArrayToInt(tmp));
                }

                dataBuf.Position = 0;

                strUIParseResult += dataBuf.GetString(2) + Name + "\r\n";
                strUIParseResult += dataBuf.GetString(2) + ": 数据长度" + Len.ToString() + "\r\n";

                strUIParseResult = dataBuf.GetString(4) + " 当前网络单元ID:" + networkUnit.CurrentID.ToString() + "\r\n";
                strUIParseResult += dataBuf.GetString(1) + " 邻域网络单元ID个数:" + networkUnit.NeighborIDs.Count.ToString() + "\r\n";

                for (int i = 0; i < num; i++)
                {
                    strUIParseResult += dataBuf.GetString(4) + " 邻域网络单元ID" + (i + 1).ToString() + ":" + networkUnit.NeighborIDs[i].ToString() + "\r\n";
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            // 可以在派生类中重写该方法
            return true;
        }

    }
    #endregion

    #region 0012 ICCID 身份信息集的集成电路识别码  ICCID是指集成电路卡识别码（智能卡卡号），也就是SIM卡卡号，由20个字符组成（19位数字+最后1位英文字母）。它相当于你手机号的身份证号，包含着所属运营商、归属地等重要信息。

    public class ICCID
    {
        public int Len;
        public string strValue { get; set; }
        public byte[] byteValue { get; set; }

        public bool Parser()
        {
            try
            {
                strValue = Encoding.ASCII.GetString(byteValue);
                strValue = strValue.Replace("\0", "");

                strValue = GXCommon.ReverseString(strValue);
            }
            catch (Exception ex)
            {
                return false;
            }
            // 可以在派生类中重写该方法
            return true;
        }
    }

    public class GXGXAliceChannelFeature_0013 : GXAliceChannelFeature
    {
        #region 属性

        public ICCID iccid { get; set; } = new ICCID();
        #endregion

        public GXGXAliceChannelFeature_0013()
        {
            Code = 0x0013;
            Name = "信道特征信息编码0013 ICCID  身份信息集成电路识别码(SIM卡卡号)";
        }


        public override bool Parser()
        {
            try
            {
                dataBuf.Position = 0;
                Code = dataBuf.GetUInt16();
                Len = dataBuf.GetUInt16();

                iccid.Len = dataBuf.GetUInt8();
                iccid.byteValue = new byte[iccid.Len];
                dataBuf.Get(iccid.byteValue);

                iccid.Parser();
                dataBuf.Position = 0;
                strUIParseResult = "";

                strUIParseResult += dataBuf.GetString(2) + ":" + Name + "\r\n";
                strUIParseResult += dataBuf.GetString(2) + ":数据长度=" + Len.ToString() + "\r\n";
                strUIParseResult += dataBuf.GetString(1) + ":ICCID长度=" + iccid.Len.ToString() + "\r\n";
                strUIParseResult += dataBuf.GetString(iccid.Len) + ": ICCID =" + iccid.strValue + "\r\n";
            }
            catch (Exception ex)
            {
                return false;
            }
            // 可以在派生类中重写该方法
            return true;
        }
    }
    #endregion

    #region 附录B.2信道特征信息
    public class ChannelFeatureDirectory
    {
       
        //字段
        public Dictionary<ushort, GXAliceChannelFeature> FeatureDictionary = new Dictionary<ushort, GXAliceChannelFeature>(30);

        public ChannelFeatureDirectory()
        {
            GXAliceChannelFeature Feature0012 = new GXGXAliceChannelFeature_0012();
            GXAliceChannelFeature Feature0013 = new GXGXAliceChannelFeature_0013();

            FeatureDictionary.Add(Feature0012.Code, Feature0012);
            FeatureDictionary.Add(Feature0013.Code, Feature0013);

        }
    }
    #endregion
}